package com.lbn.monitor.app.monitor.facility;

import android.os.Handler;
import android.util.Log;

import com.free.framework.business.BaseModel;
import com.free.framework.business.NetSubscriber;
import com.free.framework.business.ServerException;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lbn.monitor.api.ApiService;
import com.lbn.monitor.api.AppHost;
import com.lbn.monitor.api.model.RealTimeValue;
import com.lbn.monitor.api.model.XiShu;
import com.lbn.monitor.base.AppBasePresenter;
import com.lbn.monitor.util.UIHandler;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 李炳南
 * 2017/3/6.
 */

public class FacilityDetailPresenter extends AppBasePresenter<IFacilityDetailView> {


    public void realTimeValue(final String mn, String deviceId) {

//        subscribeNetwork(AppHost.getApi().realTimeValue(mn), new NetSubscriber<List<RealTimeValue>>(getView(), false) {
//            @Override
//            public void onNext(List<RealTimeValue> data) {
//                getView().onSucceed(data);
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                super.onError(e);
//                UIHandler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        realTimeValue(mn);
//                    }
//                }, 2000);
//            }
//        });

        ApiService api = AppHost.getApi();
        final IFacilityDetailView v = getView();

        v.showLoadingView(true);
        Observable.concat(api.liuLiangXiShu(deviceId), api.realTimeValue(mn))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Object>() {

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if(v==null){
                            return;
                        }
                        v.hideLoadingView();
                        v.showToast(e.getMessage());
                    }

                    @Override
                    public void onNext(Object o) {
//                        Log.e("okhttp", "======>" + new Gson().toJson(o));
                        if (v == null) {
                            return;
                        }
                        BaseModel<List> data = (BaseModel<List>) o;
                        if (data.data != null && data.data.size() > 0) {
                            Object object = data.data.get(0);
                            if (object instanceof XiShu) {
                                v.onXiShuSucceed((XiShu) object);
                            } else if (object instanceof RealTimeValue) {
                                v.onSucceed((List<RealTimeValue>) data.data);
                            }
                        }
                    }
                });
    }


    public void liuLiangXiShu(String deviceId) {


        subscribeNetwork(AppHost.getApi().liuLiangXiShu(deviceId), new NetSubscriber<List<XiShu>>(getView()) {
            @Override
            public void onNext(List<XiShu> data) {
                getView().onXiShuSucceed(data.get(0));
            }

            @Override
            public void onError(Throwable e) {
//                super.onError(e);
//                getView().showToast("系数获取失败，请重新获取");
            }
        });

    }

}
